<?= "<?php\n" ?>
declare(strict_types=1);

namespace <?= $namespace ?>Entity;

use <?= $namespace ?>Model\<?= $modelName ?>;
use SixShop\Core\Entity\BaseEntity;
use think\Collection;
use think\Paginator;
use function SixShop\Core\throw_logic_exception;

/**
 * <?= $tableComment ?: $entityName . ' Entity' ?>
<?php if (!empty($tableComment)): ?>
 * 
 * Table: <?= $tableName ?> - <?= $tableComment ?>
<?php endif; ?>
 * 
 * @package <?= $namespace ?>Entity
 */
class <?= $entityName ?> extends BaseEntity
{
<?php 
$methodName = ucfirst(str_replace('_', '', $tableName));
// Generate singular method name based on table name
$singularMethodName = $methodName;
if (str_ends_with($methodName, 's') && !str_ends_with($methodName, 'ss')) {
    $singularMethodName = substr($methodName, 0, -1);
}
?>
    /**
     * Display a listing of the resource
     */
    public function index<?= $singularMethodName ?>(array $params, array $pageAndLimit = []):Paginator| Collection
    {
        $query = $this->withSearch([
            'id',
            // ... 请根据实际情况完成需要搜索的字段
        ], $params);
        if (!empty($pageAndLimit)) {
            return $query->paginate($pageAndLimit);
        } else {
            return $query->select();
        }
    }
    
    /**
     * Show the form for creating a new resource
     */
    public function create<?= $singularMethodName ?>(): array
    {
        return [
            // ... 请根据实际情况完成新建数据的表单字段
        ];
    }
    
    /**
     * Store a newly created resource in storage
     */
    public function save<?= $singularMethodName ?>(array $data): <?= $modelName ?>
    {
        $model = new <?= $modelName ?>();
        $model->save($data);
        return $model;
    }
    
    /**
     * Display the specified resource
     */
    public function read<?= $singularMethodName ?>(int $id):self
    {
        return $this->find($id);
    }
    
    /**
     * Show the form for editing the specified resource
     */
    public function edit<?= $singularMethodName ?>(int $id): array
    {
        // TODO: Implement edit form logic
        return [
            // ... 请根据实际情况完成编辑数据的表单字段
        ];
    }
    
    /**
     * Update the specified resource in storage
     */
    public function update<?= $singularMethodName ?>(int $id, array $params): self
    {
        $entity = $this->findOrEmpty($id);
        if ($entity->isEmpty()) {
            throw_logic_exception('数据不存在');
        }
        $entity->save($params);
        return $entity;
    }
    
    /**
     * Remove the specified resource from storage
     */
    public function delete<?= $singularMethodName ?>(int $id): bool
    {
        $entity = $this->findOrEmpty($id);
        if ($entity->isEmpty()) {
            throw_logic_exception('数据不存在');
        }
        return $entity->delete();
    }
}